package com.jy.datapipeline.export.controller;

import com.alibaba.fastjson.JSONObject;
import com.jy.datapipeline.export.common.constants.SplitKeyExecuteConstant;
import com.jy.datapipeline.export.common.enums.impl.SplitKeyExecutorEnums;
import com.jy.datapipeline.export.service.ExpRuleDetailService;
import com.jy.datapipeline.export.entity.ExpRuleDetailModel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
    @RequestMapping(value = "/exprule/detail/")
public class ExpRuleDetailController {
    @Autowired
    private ExpRuleDetailService expRuleDetailService;


    @RequestMapping( value = "findByList",method = RequestMethod.GET)
    @ResponseBody
    public Object findByList(
        @RequestParam(value = "ruleId",required = true) String ruleId,
        @RequestParam(value = "schemaName",required = true) String schemaName,
        @RequestParam(value = "ruleTbl",required = true) String ruleTbl,
        @RequestParam(value = "iDisplayStart",required = true) Integer iDisplayStart,
        @RequestParam(value = "iDisplayLength",required = true) Integer iDisplayLength
    ){
        Map<String, Object> map=new HashMap<>();
        List<Map<String,Object>> list=this.expRuleDetailService.findExpRuleDetailByList(ruleId,schemaName,ruleTbl,iDisplayStart,iDisplayLength);
        Integer count=this.expRuleDetailService.findExpRuleDetailByCount(ruleId,schemaName,ruleTbl);
        map.put("datalist",list);
        map.put("count", count);
        return map;
    }

    @RequestMapping( value = "add",method = RequestMethod.POST)
    @ResponseBody
    public Object add(@RequestBody JSONObject  jsonObject){
        Map<String, Object> result=new HashMap<>();
        try{
            ExpRuleDetailModel erdm=new ExpRuleDetailModel();
            String ruleId=jsonObject.getString("ruleId");
            String collist=jsonObject.getString("collist");
            String ruleTbl=jsonObject.getString("ruleTbl");
            String rule_tbl_suffix=jsonObject.getString("rule_tbl_suffix");
            int ruleType=Integer.parseInt(jsonObject.getString("ruleType"));

            int initRow=Integer.parseInt(jsonObject.getString("init_row"));
            int rule_tbl_type=Integer.parseInt(jsonObject.getString("rule_tbl_type"));
            int data_period=Integer.parseInt(jsonObject.getString("data_period"));
            int fun_value=Integer.parseInt(jsonObject.get("fun_value")==null?"0":jsonObject.get("fun_value").toString());
            String data_unit=jsonObject.getString("data_unit");
            String rowRange=jsonObject.getString("rowRange");
            String schemaName=jsonObject.getString("schemaName");
            String splitKey=jsonObject.getString("split_key");
            //String unionTableName=jsonObject.getString("union_table_name");
            erdm.setRuleId(ruleId);
            erdm.setRuleTblType(rule_tbl_type);
            erdm.setRuleTblSuffix(rule_tbl_suffix);
            erdm.setColList(collist);
            erdm.setRuleTbl(ruleTbl);
            erdm.setRuleTYpe(ruleType);
            erdm.setRowRange(rowRange);
            erdm.setSchemaName(schemaName);
            erdm.setInitRow(initRow);
            erdm.setDataPeriod(data_period);
            erdm.setDataUnit(data_unit);
            erdm.setFunValue(fun_value);
            erdm.setSplitKey(splitKey);
            erdm.setIsAccurate(SplitKeyExecutorEnums.ROUGH_SPLIT_EXECUTOR.getCode()); //默认添加粗略分片键
            //erdm.setUnionTableName(unionTableName);
            ExpRuleDetailModel erdmInfo=  this.expRuleDetailService.findByRuleIdAndSchemaNameAndRuleTbl(ruleId,schemaName,ruleTbl);
            if(erdmInfo==null){
                this.expRuleDetailService.save(erdm);
                result.put("status",true);
                result.put("msg","success");
            }else {
                result.put("status",false);
                result.put("msg",ruleTbl+"表已经存在了");
            }
        }catch (Exception e){
            log.error("add exception",e);
            result.put("msg","error ["+e.getMessage()+"]");
            result.put("status",false);
        }
        return result;
    }

    @RequestMapping( value = "update",method = RequestMethod.POST)
    @ResponseBody
    public Object update(@RequestBody JSONObject  jsonObject){
        Map<String, Object> result=new HashMap<>();
        try{
            ExpRuleDetailModel erdm=new ExpRuleDetailModel();
            String ruleId=jsonObject.getString("ruleId");
            String collist=jsonObject.getString("collist");
            String ruleTbl=jsonObject.getString("ruleTbl");
            String rule_tbl_suffix=jsonObject.getString("rule_tbl_suffix");
            int ruleType=Integer.parseInt(jsonObject.getString("ruleType"));
            int rule_tbl_type=Integer.parseInt(jsonObject.getString("rule_tbl_type"));
            int id=Integer.parseInt(jsonObject.getString("id"));
            int initRow=Integer.parseInt(jsonObject.getString("init_row"));
            String rowRange=jsonObject.getString("rowRange");
            String schemaName=jsonObject.getString("schemaName");
            String ruleDetailId=jsonObject.getString("ruleDetailId");
            int data_period=Integer.parseInt(jsonObject.getString("data_period"));
            String data_unit=jsonObject.getString("data_unit");

            int fun_value=Integer.parseInt(jsonObject.get("fun_value")==null?"0":jsonObject.get("fun_value").toString());
            String splitKey=jsonObject.getString("split_key");
           // String unionTableName=jsonObject.getString("union_table_name");
            erdm.setRuleId(ruleId);
            erdm.setColList(collist);
            erdm.setRuleTblSuffix(rule_tbl_suffix);
            erdm.setRuleTblType(rule_tbl_type);
            erdm.setRuleTbl(ruleTbl);
            erdm.setRuleTYpe(ruleType);
            erdm.setRowRange(rowRange);
            erdm.setSchemaName(schemaName);
            erdm.setRuleDetailId(ruleDetailId);
            erdm.setId(id);
            erdm.setInitRow(initRow);
            erdm.setDataPeriod(data_period);
            erdm.setDataUnit(data_unit);
            erdm.setFunValue(fun_value);
            erdm.setSplitKey(splitKey);
            //erdm.setUnionTableName(unionTableName);
            boolean isUpdate=false;
            ExpRuleDetailModel erdmInfo=this.expRuleDetailService.findByRuleDetailId(ruleDetailId);
            //ExpRuleDetailModel erdmInfo=  this.expRuleDetailService.findByRuleIdAndSchemaNameAndRuleTbl(ruleId,schemaName,ruleTbl);
            if(erdmInfo!=null){
                isUpdate=this.expRuleDetailService.updateExpRuleDetail(erdm);
                result.put("status",isUpdate);
                result.put("msg",isUpdate?"success":"修改失败");
            }else{
                result.put("status",false);
                result.put("msg","信息不存在");
            }


        }catch (Exception e){
            log.error("update exception");
            result.put("msg","error ["+e.getMessage()+"]");
            result.put("status",false);
        }
        return result;
    }

    @RequestMapping( value = "deleteById",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteById(
        @RequestParam(value = "id",required = true) int id
    ){
        Map<String, Object> result=new HashMap<>();
        try{

            boolean flag=this.expRuleDetailService.deleteById(id);
            result.put("status",flag);
            result.put("msg","success");
        }catch (Exception e){
            log.error("deleteById exception");
            result.put("msg","error ["+e.getMessage()+"]");
            result.put("status",false);
        }
        return result;
    }


    @RequestMapping( value = "findByRuleDetailId",method = RequestMethod.GET)
    @ResponseBody
    public Object findByRuleDetailId(
        @RequestParam(value = "ruleDetailId",required = true) String ruleDetailId
    ){
        Map<String, Object> result=new HashMap<>();
        try{
            ExpRuleDetailModel erdm= this.expRuleDetailService.findByRuleDetailId(ruleDetailId);
            result.put("data",erdm);
            result.put("status",true);
            result.put("msg","success");
        }catch (Exception e){
            log.error("findAll exception");
            result.put("msg","error ["+e.getMessage()+"]");
            result.put("status",false);
        }
        return result;
    }
}
